/*
 * @Author: 彭林
 * @Date: 2021-07-13 17:13:06
 * @LastEditTime: 2021-07-14 12:34:25
 * @LastEditors: 彭林
 * @Description:
 * @FilePath: /saas-app/app/service/yjj_five_data.go
 */
package service

import (
	"saas-app/app/dao"
	"saas-app/app/model"
	"strconv"
	"strings"

	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gtime"
)

const url2 = "http://vip.lkag3.com/K25ffa67b115f46/ptxffc-10.json"
const url3 = "http://vip.lkag3.com/K25ffa67b115f46/hn60-10.json"

type FiveDataRepository interface {
	GetFiveData(url string) (string, error)
	GetAllData() (map[string]*gjson.Json, error)
	Batch() error
}

type fiveData struct{}

func FiveDataService() FiveDataRepository {
	return &fiveData{}
}

func (f fiveData) GetFiveData(url string) (string, error) {
	c := g.Client()
	c.SetHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36")
	if r, e := c.Get(url); e != nil {
		return "", e
	} else {
		r.Close()
		return r.ReadAllString(), nil
	}
}

func (f fiveData) GetAllData() (map[string]*gjson.Json, error) {

	arr2, err := f.GetFiveData(url2)

	if err != nil {
		return nil, err
	}

	arr3, err := f.GetFiveData(url3)

	if err != nil {
		return nil, err
	}

	result := make(map[string]*gjson.Json)

	result["arr2"] = gjson.New(arr2)
	result["arr3"] = gjson.New(arr3)

	return result, nil
}

func (f fiveData) Batch() error {

	result, err := f.GetAllData()
	if err != nil {
		return err
	}

	arr2 := result["arr2"].Array()[0].(map[string]interface{})
	arr3 := make(map[string]interface{})
	opendate := arr2["opendate"]
	arr3Data := result["arr3"].Array()

	for _, v := range arr3Data {
		value := v.(map[string]interface{})
		if value["opendate"].(string) == opendate {
			arr3 = v.(map[string]interface{})
		}
	}

	err1 := f.insertData(arr2, "2")
	err2 := f.insertData(arr3, "3")

	go initCurrentData(arr2, arr3)

	if err1 != nil {
		return err1
	}

	if err2 != nil {
		return err2
	}
	return nil
}

func (f fiveData) insertData(obj map[string]interface{}, num string) error {
	var fiveData model.YjjFiveData
	fiveData.Num = num
	fiveData.Dates = forMatTime(obj["opendate"].(string))
	fiveData.Data = obj["code"].(string)
	fiveData.Last = getLastData(obj["code"].(string))
	fiveData.CreateTime = uint(gtime.Now().Timestamp())
	_, err := dao.YjjFiveData.Data(fiveData).Insert()
	return err
}

func initCurrentData(arr2, arr3 map[string]interface{}) error {
	dates := arr2["opendate"].(string)
	num2 := getLastData(arr2["code"].(string))
	num3 := getLastData(arr3["code"].(string))
	arr := [5]string{}
	for index, _ := range arr {
		code := "0" + string(num2[index]) + string(num3[index]) + "00"
		num := getNum(dates) + 1 + index
		insertCurrentData(dates, code, formatCurrentNum(num))
	}
	return nil
}

func insertCurrentData(opendate, code, num string) error {

	date := gtime.New(opendate).Format("Ymd")

	var fiveCurrentData model.YjjFiveCurrent
	fiveCurrentData.Date = date
	Num, _ := strconv.Atoi(num)
	fiveCurrentData.Num = uint(Num)
	fiveCurrentData.Code = code
	fiveCurrentData.CreateTime = int(gtime.Now().Timestamp())

	_, err := dao.YjjFiveCurrent.Data(fiveCurrentData).Insert()
	return err
}

func forMatTime(time string) uint {
	gt := gtime.New(time)
	return uint(gt.Timestamp())
}

func getLastData(code string) string {
	str := strings.Replace(code, ",", "", -1)
	str2 := str[len(str)-5 : len(str)-0]
	return str2
}

func getNum(time string) int {
	gt := gtime.New(time)
	h := gt.Hour()
	m := gt.Minute()
	num := (h*60 + m) * 5
	return num
}

// 格式化期数
func formatCurrentNum(num int) string {
	str := ""
	strNum := strconv.Itoa(num)
	if num < 10 {
		str = "000" + strNum
	} else if num < 100 {
		str = "00" + strNum
	} else if num < 1000 {
		str = "0" + strNum
	} else {
		str = strNum
	}
	return str
}
